﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace Proyecta.Database
{
    public class XMLLoader
    {
        private string factory;
        private string server;
        private string dataBase;
        private string userName;      
        private string password;     

        #region Singleton
        private static XMLLoader instance;

        private XMLLoader()
        {
            this.getValues();
        }

        static XMLLoader()
        {
            instance = new XMLLoader();
        }

        public static XMLLoader Default
        {
            get { return instance; }
        }
        #endregion

        public string Factory
        {
            get { return factory; }            
        }

        public string Server
        {
            get { return server; }           
        }

        public string DataBase
        {
            get { return dataBase; }           
        }

        public string Password
        {
            get { return password; }            
        }

        public string UserName
        {
            get { return userName; }            
        }

        private Hashtable getValues()
        {
            XmlDocument xmlInfo = new XmlDocument();
            try
            {
                xmlInfo.Load(AppDomain.CurrentDomain.BaseDirectory + "\\Configuracion.xml");
                XmlNode node = xmlInfo["Connection"].ChildNodes[0];
                this.server = node.ChildNodes[0].Value.ToString();
                XmlNode nodeDB = xmlInfo["Connection"].ChildNodes[1];
                this.dataBase = nodeDB.ChildNodes[0].Value.ToString();
                if (xmlInfo["Connection"].ChildNodes[2].InnerText.CompareTo("") != 0)
                {
                    XmlNode nodeUser = xmlInfo["Connection"].ChildNodes[2];
                    this.userName = nodeUser.ChildNodes[0].Value.ToString();
                }
                if (xmlInfo["Connection"].ChildNodes[3].InnerText.CompareTo("") != 0)
                {
                    XmlNode nodePass = xmlInfo["Connection"].ChildNodes[3];
                    this.password = nodePass.ChildNodes[0].Value.ToString();
                }

                XmlNode nodeFactory = xmlInfo["Connection"].ChildNodes[4];
                this.factory = nodeFactory.ChildNodes[0].Value.ToString();

                Hashtable ht = new Hashtable();
                ht["DbProviderFactory"] = factory;
                ht["Server"] = server;
                ht["DataBase"] = dataBase;
                ht["UserName"] = userName;
                ht["Password"] = password;

                return ht;
            }
            catch { throw; }
        }
    }
}
